<template>
  <div>
    <div class="no-print">
      <span style="display: inline-block; padding: 5px 20px;font-size: 1.2em;">列表显示</span>
      <el-checkbox label="血液大类" v-model="showXyzl" />
      <el-checkbox label="血液品种" v-model="showPinZhong" />
      <el-checkbox label="规格" v-model="showSpesc" />
      <el-checkbox label="血量" v-model="showRongLiang" />
      <el-checkbox label="袋数" v-model="showAmount" />
      <el-checkbox label="金额" v-model="showJiaGe" />
    </div>
    <div v-if="pinZhongMingChengList.length > 0" class="no-print">
      <span style="display: inline-block; padding: 5px 20px;font-size: 1.2em;">血液品种</span>
      <el-checkbox-group v-model="checkPinZhongList" style="display: inline-block;">
        <template v-for="item in pinZhongMingChengList">
          <el-checkbox :label="item" />
        </template>
      </el-checkbox-group>
    </div>
    <MkTable :data="showList" :span-method="objectSpanMethod">
      <MkColumn label="血液大类" align="center" prop="daLei" v-if="showXyzl" />
      <MkColumn label="血液品种" align="center" prop="pinZhongMingCheng" v-if="showPinZhong" />
      <MkColumn label="规格" align="center" prop="spesc" v-if="showSpesc" width="80px" />
      <el-table-column label="A" align="center">
        <el-table-column label="血量" prop="aRongLiang" minWidth="50px" align="center" v-if="showRongLiang" />
        <el-table-column label="袋数" prop="aAmount" minWidth="50px" align="center" v-if="showAmount" />
        <el-table-column label="金额" prop="aJiaGe" minWidth="60px" align="center" v-if="showJiaGe" />
      </el-table-column>
      <el-table-column label="B" align="center">
        <el-table-column label="血量" prop="bRongLiang" minWidth="50px" align="center" v-if="showRongLiang" />
        <el-table-column label="袋数" prop="bAmount" minWidth="50px" align="center" v-if="showAmount" />
        <el-table-column label="金额" prop="bJiaGe" minWidth="60px" align="center" v-if="showJiaGe" />
      </el-table-column>
      <el-table-column label="AB" align="center">
        <el-table-column label="血量" prop="abRongLiang" minWidth="50px" align="center" v-if="showRongLiang" />
        <el-table-column label="袋数" prop="abAmount" minWidth="50px" align="center" v-if="showAmount" />
        <el-table-column label="金额" prop="abJiaGe" minWidth="60px" align="center" v-if="showJiaGe" />
      </el-table-column>
      <el-table-column label="O" align="center">
        <el-table-column label="血量" prop="oRongLiang" minWidth="50px" align="center" v-if="showRongLiang" />
        <el-table-column label="袋数" prop="oAmount" minWidth="50px" align="center" v-if="showAmount" />
        <el-table-column label="金额" prop="oJiaGe" minWidth="60px" align="center" v-if="showJiaGe" />
      </el-table-column>
      <el-table-column label="总计" align="center">
        <el-table-column label="血量" prop="totalRongLiang" minWidth="60px" align="center" v-if="showRongLiang" />
        <el-table-column label="袋数" prop="totalAmount" minWidth="60px" align="center" v-if="showAmount" />
        <el-table-column label="金额" prop="totalJiaGe" minWidth="80px" align="center" v-if="showJiaGe" />
      </el-table-column>
    </MkTable>
  </div>
</template>

<script lang='ts' setup>
import { BasXueYePinZhongAllOutput } from '/@/api/bas/BasXueYePinZhong';
import { xueYePinZhongList } from '/@/stores/baseInfo/';

defineOptions({
  name: 'invKuCunHuiZong'
})

// 品种汇总接口
interface PinZhongSummaryType {
  daLei: string
  pinZhongId: number
  pinZhongMingCheng: string
  rongLiang: number
  spesc: string
  aAmount: number
  aRongLiang: number
  aJiaGe: number
  bAmount: number
  bRongLiang: number
  bJiaGe: number
  abAmount: number
  abRongLiang: number
  abJiaGe: number
  oAmount: number
  oRongLiang: number
  oJiaGe: number
  totalAmount: number
  totalRongLiang: number
  totalJiaGe: number
}


const showXyzl = ref(true) // 显示大类
const showPinZhong = ref(true) // 显示品种
const showSpesc = ref(true) // 显示规格
const showRongLiang = ref(false)
const showAmount = ref(true)
const showJiaGe = ref(false)
// 获取品种名称
const getPinZhongMingCheng = (pinZhongId: number) => {
  if (pinZhongId == 0) {
    return '未知品种'
  }
  const pinZhong = xueYePinZhongList.value.find((item: BasXueYePinZhongAllOutput) => item.id == pinZhongId)
  return pinZhong?.mingCheng ?? pinZhongId
}
// 获取大类
const getDaLei = (pinZhongId: number) => {
  if (pinZhongId == 0) {
    return '未知大类'
  }
  const pinZhong = xueYePinZhongList.value.find((item: BasXueYePinZhongAllOutput) => item.id == pinZhongId)
  return pinZhong?.daLei ?? ''
}
type XueYeType = {
  xueYeId?: number;
  xianXueMa: string;
  chanPinId: string;
  mingCheng: string;
  abo: string;
  rhD: string;
  rongLiang: number;
  danWei: string;
  shiXiaoShiJian: string;
  pinZhongId?: number
  daLei: string;
  jiaGe: number;
};
const props = defineProps({
  bloodList: { // 血液列表
    type: Array as PropType<XueYeType[]>,
    default: () => []
  }
})

const list = ref<PinZhongSummaryType[]>([]);

// 根据传入的 bloodList 动态生成 list
const generateList = () => {
  // 修改 tempMap 的键类型，使用字符串来组合 pinZhongId 和 spesc
  const tempMap: { [key: string]: PinZhongSummaryType } = {};
  props.bloodList.forEach((blood: XueYeType) => {
    // 生成组合键
    const pinZhongId = blood.pinZhongId ?? 0;
    const key = `${pinZhongId}-${blood.rongLiang}${blood.danWei}`;
    if (!tempMap[key]) {
      tempMap[key] = {
        daLei: getDaLei(pinZhongId),
        pinZhongId: pinZhongId,
        pinZhongMingCheng: getPinZhongMingCheng(pinZhongId) as string,
        rongLiang: blood.rongLiang,
        spesc: blood.rongLiang + blood.danWei, // 这里暂时留空，可根据实际情况修改
        aAmount: 0,
        aRongLiang: 0,
        aJiaGe: 0,
        bAmount: 0,
        bRongLiang: 0,
        bJiaGe: 0,
        abAmount: 0,
        abRongLiang: 0,
        abJiaGe: 0,
        oAmount: 0,
        oRongLiang: 0,
        oJiaGe: 0,
        totalAmount: 0,
        totalRongLiang: 0,
        totalJiaGe: 0
      };
    }

    const item = tempMap[key];
    switch (blood.abo) {
      case 'A':
        item.aAmount += 1;
        item.aRongLiang += blood.rongLiang;
        item.aJiaGe += blood.jiaGe;
        break;
      case 'B':
        item.bAmount += 1;
        item.bRongLiang += blood.rongLiang;
        item.bJiaGe += blood.jiaGe;
        break;
      case 'AB':
        item.abAmount += 1;
        item.abRongLiang += blood.rongLiang;
        item.abJiaGe += blood.jiaGe;
        break;
      case 'O':
        item.oAmount += 1;
        item.oRongLiang += blood.rongLiang;
        item.oJiaGe += blood.jiaGe;
        break;
    }

    item.totalAmount += 1;
    item.totalRongLiang += blood.rongLiang;
    item.totalJiaGe += blood.jiaGe;
    // 这里金额暂时未处理，可根据实际情况添加逻辑
  });

  list.value = Object.values(tempMap).sort((a, b) => {
    if (a.daLei !== b.daLei) {
      return a.daLei.localeCompare(b.daLei); // 按 daLei 排序
    }
    // 先按 pinZhongId 排序
    if (a.pinZhongId !== b.pinZhongId) {
      return a.pinZhongId - b.pinZhongId;
    }
    // 若 pinZhongId 相同，再按容量排序
    return a.rongLiang - b.rongLiang;
  });
};

watchEffect(() => {
  console.log('bloodList', props.bloodList);
  generateList();
})
// 勾选品种数组
const checkPinZhongList = ref<string[]>([])
// 获取品种列表
const pinZhongMingChengList = computed(() => {
  const set = new Set<string>();
  list.value.forEach(item => set.add(item.pinZhongMingCheng));
  const result = Array.from(set)
  checkPinZhongList.value = result
  return result
})
const xyzlSpanArr = ref<number[]>([])
const PinZhongSpanArr = ref<number[]>([])

// 计算合并信息
const calculateSpan = (list: any) => {
  xyzlSpanArr.value = [];
  PinZhongSpanArr.value = [];
  let xyzlPosition = 0;
  let PinZhongPosition = 0
  list.forEach((item: any, index: number) => {
    if (index === 0) {
      xyzlSpanArr.value.push(1);
      xyzlPosition = 0;

      PinZhongSpanArr.value.push(1);
      PinZhongPosition = 0;
    } else {
      // 判断当前行的 daLei 是否和上一行相同
      if (item.daLei === list[index - 1].daLei) {
        xyzlSpanArr.value[xyzlPosition]++;
        xyzlSpanArr.value.push(0);
      } else {
        xyzlSpanArr.value.push(1);
        xyzlPosition = index;
      }
      if (item.pinZhongMingCheng === list[index - 1].pinZhongMingCheng) {
        PinZhongSpanArr.value[PinZhongPosition]++;
        PinZhongSpanArr.value.push(0);
      } else {
        PinZhongSpanArr.value.push(1);
        PinZhongPosition = index;
      }
    }
  });
}
// 定义 objectSpanMethod 函数的参数类型
interface SpanMethodParams {
  row: any;
  column: any; // 这里可以根据实际情况细化 column 的类型
  rowIndex: number;
  columnIndex: number;
}
// 合并单元格的方法
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }: SpanMethodParams) => {
  if (columnIndex === 0 && column.label == '血液大类') {
    const rowspan = xyzlSpanArr.value[rowIndex];
    return {
      rowspan: rowspan,
      colspan: 1
    };
  }
  if ((columnIndex === 0 || columnIndex == 1) && column.label == '血液品种') {
    const rowspan = PinZhongSpanArr.value[rowIndex];
    return {
      rowspan: rowspan,
      colspan: 1
    };
  }
}
watchEffect(() => {
  // 不显示大类、不显示品种、显示规格  -- 不给这样子选
  if (!showPinZhong.value && !showXyzl.value) showSpesc.value = false
})
const showList = computed(() => {
  let returnList = list.value.filter(item => {
    return checkPinZhongList.value.indexOf(item.pinZhongMingCheng) > -1
  })

  const dataList = list.value.filter(item => {
    return pinZhongMingChengList.value.indexOf(item.pinZhongMingCheng) > -1
  })
  // 不显示大类、不显示品种、显示规格 ？ -- 不给这样子选
  // 不显示大类、显示品种、显示规格 -- 不用过滤
  // 显示大类、显示品种、显示规格 -- 不用过滤
  // 不显示大类、不显示品种、不显示规格 -- 直接汇总不同的血量、袋数、金额，（xyzlName非血浆的血量*200）
  if (!showXyzl.value && !showPinZhong.value && !showSpesc.value) {
    let result: PinZhongSummaryType[] = [];
    let aRongLiang = 0
    let bRongLiang = 0
    let abRongLiang = 0
    let oRongLiang = 0
    let aAmount = 0
    let bAmount = 0
    let abAmount = 0
    let oAmount = 0
    let aJiaGe = 0
    let bJiaGe = 0
    let abJiaGe = 0
    let oJiaGe = 0
    let totalAmount = 0
    let totalRongLiang = 0
    let totalJiaGe = 0
    dataList.forEach(item => {
      if (item.daLei !== '血浆') {
        aRongLiang += item.aRongLiang * 200;
        bRongLiang += item.bRongLiang * 200;
        abRongLiang += item.abRongLiang * 200;
        oRongLiang += item.oRongLiang * 200;
      } else {
        aRongLiang += item.aRongLiang;
        bRongLiang += item.bRongLiang;
        abRongLiang += item.abRongLiang;
        oRongLiang += item.oRongLiang;
      }
      aAmount += item.aAmount
      bAmount += item.bAmount
      abAmount += item.abAmount
      oAmount += item.oAmount
      aJiaGe += item.aJiaGe
      bJiaGe += item.bJiaGe
      abJiaGe += item.abJiaGe
      oJiaGe += item.oJiaGe
      totalAmount += item.totalAmount;
      totalRongLiang += item.daLei == '血浆' ? item.totalRongLiang : (item.totalRongLiang * 200);
      totalJiaGe += item.totalJiaGe;
    });
    result.push({
      daLei: '',
      pinZhongId: 0,
      pinZhongMingCheng: '',
      spesc: '',
      aAmount,
      aRongLiang,
      aJiaGe,
      bAmount,
      bRongLiang,
      bJiaGe,
      abAmount,
      abRongLiang,
      abJiaGe,
      oAmount,
      oRongLiang,
      oJiaGe,
      totalAmount,
      totalRongLiang,
      totalJiaGe,
      rongLiang: 0
    });
    returnList = result
  }
  // 不显示大类、显示品种、不显示规格 -- 相同pinZhongMingCheng的血量相加、袋数相加、金额相加
  else if (!showXyzl.value && showPinZhong.value && !showSpesc.value) {
    const map: { [key: string]: PinZhongSummaryType } = {};
    dataList.forEach(item => {
      const key = item.pinZhongMingCheng;
      console.log(map[key]);

      if (!map[key]) {
        map[key] = {
          ...item,

          aAmount: item.aAmount,
          aRongLiang: item.aRongLiang,
          aJiaGe: item.aJiaGe,
          bAmount: item.bAmount,
          bRongLiang: item.bRongLiang,
          bJiaGe: item.bJiaGe,
          abAmount: item.abAmount,
          abRongLiang: item.abRongLiang,
          abJiaGe: item.abJiaGe,
          oAmount: item.oAmount,
          oRongLiang: item.oRongLiang,
          oJiaGe: item.oJiaGe,
          totalAmount: item.totalAmount,
          totalRongLiang: item.totalRongLiang,
          totalJiaGe: item.totalJiaGe
        };
      } else {
        map[key].aAmount += item.aAmount;
        map[key].aRongLiang += item.aRongLiang;
        map[key].aJiaGe += item.aJiaGe;
        map[key].bAmount += item.bAmount;
        map[key].bRongLiang += item.bRongLiang;
        map[key].bJiaGe += item.bJiaGe;
        map[key].abAmount += item.abAmount;
        map[key].abRongLiang += item.abRongLiang;
        map[key].abJiaGe += item.abJiaGe;
        map[key].oAmount += item.oAmount;
        map[key].oRongLiang += item.oRongLiang;
        map[key].oJiaGe += item.oJiaGe;
        map[key].totalAmount += item.totalAmount;
        map[key].totalRongLiang += item.totalRongLiang;
        map[key].totalJiaGe += item.totalJiaGe;
      }
    });
    console.log(map);
    console.log(Object.values(map));

    returnList = Object.values(map);
  }
  // 显示大类、不显示品种、不显示规格  -- 相同xyzlNamed的血量相加、袋数相加、金额相加
  else if (showXyzl.value && !showPinZhong.value && !showSpesc.value) {
    const map: { [key: string]: PinZhongSummaryType } = {};
    dataList.forEach(item => {
      const key = item.daLei;
      if (!map[key]) {
        map[key] = {
          ...item,
          aAmount: item.aAmount,
          aRongLiang: item.aRongLiang,
          aJiaGe: item.aJiaGe,
          bAmount: item.bAmount,
          bRongLiang: item.bRongLiang,
          bJiaGe: item.bJiaGe,
          abAmount: item.abAmount,
          abRongLiang: item.abRongLiang,
          abJiaGe: item.abJiaGe,
          oAmount: item.oAmount,
          oRongLiang: item.oRongLiang,
          oJiaGe: item.oJiaGe,
          totalAmount: item.totalAmount,
          totalRongLiang: item.totalRongLiang,
          totalJiaGe: item.totalJiaGe
        };
      } else {
        map[key].aAmount += item.aAmount;
        map[key].aRongLiang += item.aRongLiang;
        map[key].aJiaGe += item.aJiaGe;
        map[key].bAmount += item.bAmount;
        map[key].bRongLiang += item.bRongLiang;
        map[key].bJiaGe += item.bJiaGe;
        map[key].abAmount += item.abAmount;
        map[key].abRongLiang += item.abRongLiang;
        map[key].abJiaGe += item.abJiaGe;
        map[key].oAmount += item.oAmount;
        map[key].oRongLiang += item.oRongLiang;
        map[key].oJiaGe += item.oJiaGe;
        map[key].totalAmount += item.totalAmount;
        map[key].totalRongLiang += item.totalRongLiang;
        map[key].totalJiaGe += item.totalJiaGe;
      }
    });
    returnList = Object.values(map);
  }

  // 显示大类、不显示品种、显示规格 -- 相同xyzlName、相同specs的血量相加、袋数相加、金额相加
  else if (showXyzl.value && !showPinZhong.value && showSpesc.value) {
    const map: { [key: string]: PinZhongSummaryType } = {};
    dataList.forEach(item => {
      const key = `${item.daLei}-${item.spesc}`;
      if (!map[key]) {
        map[key] = {
          ...item,
          aAmount: item.aAmount,
          aRongLiang: item.aRongLiang,
          aJiaGe: item.aJiaGe,
          bAmount: item.bAmount,
          bRongLiang: item.bRongLiang,
          bJiaGe: item.bJiaGe,
          abAmount: item.abAmount,
          abRongLiang: item.abRongLiang,
          abJiaGe: item.abJiaGe,
          oAmount: item.oAmount,
          oRongLiang: item.oRongLiang,
          oJiaGe: item.oJiaGe,
          totalAmount: item.totalAmount,
          totalRongLiang: item.totalRongLiang,
          totalJiaGe: item.totalJiaGe
        };
      } else {
        map[key].aAmount += item.aAmount;
        map[key].aRongLiang += item.aRongLiang;
        map[key].aJiaGe += item.aJiaGe;
        map[key].bAmount += item.bAmount;
        map[key].bRongLiang += item.bRongLiang;
        map[key].bJiaGe += item.bJiaGe;
        map[key].abAmount += item.abAmount;
        map[key].abRongLiang += item.abRongLiang;
        map[key].abJiaGe += item.abJiaGe;
        map[key].oAmount += item.oAmount;
        map[key].oRongLiang += item.oRongLiang;
        map[key].oJiaGe += item.oJiaGe;
        map[key].totalAmount += item.totalAmount;
        map[key].totalRongLiang += item.totalRongLiang;
        map[key].totalJiaGe += item.totalJiaGe;
      }
    });
    returnList = Object.values(map);
  }
  // 显示大类、显示品种、不显示规格 -- 相同pinZhongMingCheng的血量相加、袋数相加、金额相加
  else if (showXyzl.value && showPinZhong.value && !showSpesc.value) {
    const map: { [key: string]: PinZhongSummaryType } = {};
    dataList.forEach(item => {
      const key = item.pinZhongMingCheng;
      if (!map[key]) {
        map[key] = {
          ...item,
          aAmount: item.aAmount,
          aRongLiang: item.aRongLiang,
          aJiaGe: item.aJiaGe,
          bAmount: item.bAmount,
          bRongLiang: item.bRongLiang,
          bJiaGe: item.bJiaGe,
          abAmount: item.abAmount,
          abRongLiang: item.abRongLiang,
          abJiaGe: item.abJiaGe,
          oAmount: item.oAmount,
          oRongLiang: item.oRongLiang,
          oJiaGe: item.oJiaGe,
          totalAmount: item.totalAmount,
          totalRongLiang: item.totalRongLiang,
          totalJiaGe: item.totalJiaGe
        };
      } else {
        map[key].aAmount += item.aAmount;
        map[key].aRongLiang += item.aRongLiang;
        map[key].aJiaGe += item.aJiaGe;
        map[key].bAmount += item.bAmount;
        map[key].bRongLiang += item.bRongLiang;
        map[key].bJiaGe += item.bJiaGe;
        map[key].abAmount += item.abAmount;
        map[key].abRongLiang += item.abRongLiang;
        map[key].abJiaGe += item.abJiaGe;
        map[key].oAmount += item.oAmount;
        map[key].oRongLiang += item.oRongLiang;
        map[key].oJiaGe += item.oJiaGe;
        map[key].totalAmount += item.totalAmount;
        map[key].totalRongLiang += item.totalRongLiang;
        map[key].totalJiaGe += item.totalJiaGe;
      }
    });
    returnList = Object.values(map);
  }
  returnList = JSON.parse(JSON.stringify(returnList))
  returnList.forEach(item => {
    Object.keys(item).forEach(key => {
      if ((item as any)[key] == 0) {
        (item as any)[key] = undefined
      }
    })
  })
  calculateSpan(returnList)
  return returnList
})
</script>

<style lang='scss' scoped>
:deep(.el-table__cell) {
  padding: 1px 0;
}

:deep(.cell) {
  padding: 0 5px;
}

:deep(.el-checkbox) {
  height: 30px !important;
}
</style>